package com.jk.myimkit.imkit.utilities;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.LocaleList;

import java.util.Locale;

public class LangUtils {
    private static final String LOCALE_CONF_FILE_NAME = "locale.config";
    private static final String APP_LOCALE = "app_locale";
    private static Locale systemLocale = Locale.getDefault();

    public LangUtils() {
    }

    public static Context getConfigurationContext(Context context) {
        Resources resources = context.getResources();
        Configuration config = new Configuration(resources.getConfiguration());
        Context configurationContext = context;
        if (Build.VERSION.SDK_INT >= 24) {
            LocaleList localeList = new LocaleList(new Locale[]{getAppLocale(context).toLocale()});
            LocaleList.setDefault(localeList);
            config.setLocales(localeList);
            configurationContext = context.createConfigurationContext(config);
        }

        return configurationContext;
    }

    public static LangUtils.RCLocale getAppLocale(Context context) {
        SharedPreferences sp = context.getSharedPreferences("locale.config", 0);
        String locale = sp.getString("app_locale", "auto");
        return LangUtils.RCLocale.valueOf(locale);
    }

    public static void saveLocale(Context context, LangUtils.RCLocale locale) {
        SharedPreferences sp = context.getSharedPreferences("locale.config", 0);
        sp.edit().putString("app_locale", locale.value()).commit();
    }

    public static Locale getSystemLocale() {
        return systemLocale;
    }

    public static void setSystemLocale(Locale locale) {
        systemLocale = locale;
    }

    public static LangUtils.RCLocale getCurrentLanguage(Context context) {
        SharedPreferences sp = context.getSharedPreferences("locale.config", 0);
        String locale = sp.getString("app_locale", "auto");
        if (locale.equals("auto")) {
            return getSystemLocale().toString().equals("zh_CN") ? LangUtils.RCLocale.LOCALE_CHINA : LangUtils.RCLocale.LOCALE_US;
        } else {
            return LangUtils.RCLocale.valueOf(locale);
        }
    }

    public static class RCLocale {
        public static final LangUtils.RCLocale LOCALE_CHINA = new LangUtils.RCLocale("zh");
        public static final LangUtils.RCLocale LOCALE_US = new LangUtils.RCLocale("en");
        public static final LangUtils.RCLocale LOCALE_AUTO = new LangUtils.RCLocale("auto");
        private String rcLocale;

        private RCLocale(String rcLocale) {
            this.rcLocale = rcLocale;
        }

        public String value() {
            return this.rcLocale;
        }

        public Locale toLocale() {
            Locale locale;
            if (this.rcLocale.equals(LOCALE_CHINA.value())) {
                locale = Locale.CHINESE;
            } else if (this.rcLocale.equals(LOCALE_US.value())) {
                locale = Locale.ENGLISH;
            } else {
                locale = LangUtils.systemLocale;
            }

            return locale;
        }

        public static LangUtils.RCLocale valueOf(String rcLocale) {
            LangUtils.RCLocale locale;
            if (rcLocale.equals(LOCALE_CHINA.value())) {
                locale = LOCALE_CHINA;
            } else if (rcLocale.equals(LOCALE_US.value())) {
                locale = LOCALE_US;
            } else {
                locale = LOCALE_AUTO;
            }

            return locale;
        }
    }
}
